package com.auto.api.autoconfigure;


import com.auto.api.mapping.filter.MockFilter;
import com.auto.api.mapping.listener.MappingInitListener;
import com.auto.api.properties.AutoApiProperties;
import com.google.common.collect.Lists;
import java.util.List;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

@Configuration
@EnableConfigurationProperties(AutoApiProperties.class)
@ConditionalOnProperty(value = AutoApiProperties.OPEN_MAPPING, havingValue = "true", matchIfMissing = false)
public class AutoApiConfiguration {

    private AutoApiProperties autoApiProperties;

    public AutoApiConfiguration(AutoApiProperties autoApiProperties) {
        this.autoApiProperties = autoApiProperties;
    }

    @Bean
    @ConditionalOnProperty(value = AutoApiProperties.OPEN_MOCK, havingValue = "true", matchIfMissing = false)
    public FilterRegistrationBean  mockFilter() {
        FilterRegistrationBean mockFilter = new FilterRegistrationBean(new MockFilter());
        List<String> urlPatterns = Lists.newArrayList();
        urlPatterns.add("/*");
        mockFilter.setUrlPatterns(urlPatterns);
        return mockFilter;
    }

    @Bean
    @Order(-100)
    @ConditionalOnProperty(value = AutoApiProperties.OPEN_MAPPING, havingValue = "true", matchIfMissing = false)
    public MappingInitListener mappingInitListener() {
        return new MappingInitListener();
    }

}
